﻿/*******************************************************************************
* 
* Entry for FE Gaiden r2
* 
* 
******************************************************************************/
#include <windows.h>
#include <atlbase.h>
#include <fstream>

#include <xaudio2.h>
#ifdef _WIN64
#pragma comment (lib, "x64/dxguid.lib")
#else
#pragma comment (lib, "x86/dxguid.lib")
#endif

#include "../common/common.h"
#include "json/json.h"
#include "MemoryModule.h"
#include "lua.h"

using namespace std;

HRESULT CoCreateInstanceIsolated(
    _In_     LPCTSTR szDllPath, 
    _In_     REFCLSID rclsid, 
    _In_opt_ LPUNKNOWN pUnkOuter,
    _In_     DWORD dwClsContext, 
    _In_     REFIID riid, 
    _COM_Outptr_ _At_(*ppv, _Post_readable_size_(_Inexpressible_(varies))) LPVOID FAR* ppv);

int CALLBACK WinMain(
    _In_  HINSTANCE hInstance,
    _In_  HINSTANCE hPrevInstance,
    _In_  LPSTR lpCmdLine,
    _In_  int nCmdShow
    )
{
    GloomEngine engine;
    LOG_DEBUG("sizeof(GloomEngine)=%d", sizeof(GloomEngine));

    auto field = engine.resource()->get("sound", "field.wav");
    auto battle = engine.resource()->get("sound", "battle.wav");

    //auto field_asp = engine.audio()->create(*field.get());
    auto battle_asp = engine.audio()->create(*battle.get());

    // play backgound sound async
    //field_asp->play();

    Sleep(2000); // 2

    // play sound effect async
    battle_asp->play(false);

    Sleep(10000); // 8 + 2

    // play sound effect synchronized
    battle_asp->play(false);

    // stop the sound immediately.
    battle_asp->stop();
    //field_asp->stop();



    return 0;
}


HRESULT CoCreateInstanceIsolated(
    _In_     LPCTSTR szDllPath, 
    _In_     REFCLSID rclsid, 
    _In_opt_ LPUNKNOWN pUnkOuter,
    _In_     DWORD dwClsContext, 
    _In_     REFIID riid, 
    _COM_Outptr_ _At_(*ppv, _Post_readable_size_(_Inexpressible_(varies))) LPVOID FAR* ppv)
{
    typedef HRESULT (WINAPI *DllGetClassObjectType)(
        __in   REFCLSID rclsid,
        __in   REFIID riid,
        __out  LPVOID *ppv
        );

    HRESULT hr;
    IClassFactory* pFactory;

    ifstream is;

    is.open(szDllPath, ios::binary);
    if(is.bad())
    {
        LOG_ERROR(L"Failure opening source file, s=%s", szDllPath);
        return S_FALSE;
    }
    // get length of the input file:
    is.seekg (0, ios::end);
    auto ilen = is.tellg();
    is.seekg (0, ios::beg);

    // allocate memory:
    unsigned char* ibuff = new unsigned char [ilen];

    // read data as a block:
    is.read ((char*)ibuff, ilen);
    is.close();


    auto hLib = MemoryLoadLibrary(ibuff);
    FARPROC pFunc = MemoryGetProcAddress(hLib, "DllGetClassObject");
    hr = ((DllGetClassObjectType)pFunc)(rclsid, IID_IClassFactory, (LPVOID*)&pFactory);
    if(SUCCEEDED(hr))
    {
        hr = pFactory->CreateInstance(pUnkOuter, riid, ppv);
        pFactory->Release();
    }

    delete[] ibuff;
    //MemoryFreeLibrary(hLib);

    return hr;
}